<?php	
	header("Content-Type: text/html;charset=utf-8");
	include("function/_nosql.php"); //
	include("function/function.php");
	
	//
	
	$redirect_urls = chkstr(trim($_REQUEST['redirect_urls']));
	$URL = chkstr(trim($_REQUEST['URL']));
	$aid = chkstr(trim($_REQUEST['newsid']));
	$ty = chkstr(trim($_REQUEST['ty']));
	$user_phone = chkstr(trim($_REQUEST['user_phone']));
	$user_password = md5(md5($user_phone));
	$user_realname = chkstr(trim($_REQUEST['user_realname']));
	$user_nickname = chkstr(trim($_REQUEST['user_nickname']));
	$user_phonecode = chkstr(trim($_REQUEST['user_phonecode']));
	
	$user_idcard = chkstr(trim($_REQUEST['user_idcard']));
	$order_shangche = chkstr(trim($_REQUEST['shangche']));
	$order_no = build_order_no();
	if($_GET['yzm'] == 'val'){
		if(IS_AJAX){
			$yzms = explode(',', $_POST['yzm']);
			$yzm = $yzms[0];
			$phone = $yzms[1];
				if(empty($_SESSION['PhoneCode']))
				{
					die;
				}
				if($_SESSION['PhoneName'] == $phone)
				{
					if($_SESSION['PhoneCode'] == $yzm)
					{
						$array1[] = getClassName("kboy_user","user_realname","user_tel='$phone'");
						$array1[] = getClassName("kboy_user","user_idcard","user_tel='$phone'");
						$array1[] = getClassName("kboy_user","user_nickname","user_tel='$phone'");
						$arrays = json_encode($array1);
						if(empty($array1[0])){
							$array2[] = getClassName("kboy_user","user_realname","user_phone='$phone'");
							$array2[] = getClassName("kboy_user","user_idcard","user_phone='$phone'");
							$array2[] = getClassName("kboy_user","user_nickname","user_phone='$phone'");
							$arrays = json_encode($array2);
						}
						echo $arrays;
					}else if($yzm !=''){
						echo 1;
					}
				}
					
		}
	}
	if(!IS_AJAX){
		if($ty != 1){

			echo erro("请先阅读并同意免责声明!");

		}
		if(!isMobile($user_phone)){

			echo erro("手机号格式错误！!");

		}

		if($user_phone != $_SESSION['PhoneName']){

			echo erro("请获取验证码！");

		}
		
		if($user_phonecode != $_SESSION['PhoneCode']){

			echo erro("请正确输入短信验证码！");

		}
		if(empty($user_nickname)){
			echo erro("昵称不能为空！");
		}
		if(empty($user_realname)){
			echo erro("真实姓名不能为空！");
		}
		if(empty($user_idcard)){
			echo erro("身份证不能为空！");
		}
		if(!isCardNum($user_idcard)){
			echo erro("身份证格式错误！");
		}
		if(empty($order_shangche)){
			
			echo erro("上车地不能为空！");
		}
		$sex = substr($user_idcard,-2,1);
		if($sex%2==0){
			$sex = 0;
		}else{
			$sex = 1;
		}
		$user_phones = getClassName("kboy_user","user_phone","user_phone='$user_phone'");
		if($user_phones){
//				$user_idcard = getClassName("kboy_user","user_idcard","user_phone='$user_phone'");
//				if($user_idcard){
					$order_idcard = getClassName("kboy_order","order_idcard","order_phone='$user_phone' and aid='$aid'");
					if($order_idcard){
						$kboy_userid = getClassName("kboy_user","uid","user_phone='$user_phone'");
						$_SESSION['kboy_user'] = $user_phone;
						$_SESSION['kboy_userid'] = $kboy_userid;
						$_SESSION['kboy_leader'] = getClassName("kboy_user","user_leader","( user_email='$user_phone' or user_phone='$user_phone' )");
						setcookie("username",$user_phone,time()+3600*24*365);
   						setcookie("password",getClassName("kboy_user","user_password","user_phone='$user_phone'"),time()+3600*24*365);
						echo ok("您已报过名", "activeview.php?newsid=$aid");
					}else{
						$kboy_userid = getClassName("kboy_user","uid","user_phone='$user_phone'");
						
						$mysql = new sqlstr("kboy_user"); 
						$mysql -> set("user_realname",$user_realname);
						$mysql -> set("user_nickname",$user_nickname);
						$mysql -> set("user_sex",$sex);
						$mysql -> set("user_idcard",$user_idcard);
						$news_sql = $mysql -> updateSql("uid = '$kboy_userid'");
						$news_res = mysql_query($news_sql);
						
						$mysql = new sqlstr("kboy_order"); 
						$mysql -> set("uid",$kboy_userid,true);
						$mysql -> set("order_phone",$user_phone);
						$mysql -> set("order_no",$order_no);
						$mysql -> set("order_realname",$user_realname);
						$mysql -> set("order_sex",$sex);
						$mysql -> set("order_idcard",$user_idcard);
						//性别
						$mysql -> set("order_shangche",$order_shangche);
						$mysql -> set("aid",$aid,true);
						$mysql -> set("order_num",1);
						
						$news_sql = $mysql -> insertSql();
					
						$news_res = mysql_query($news_sql);
						if($news_res){
							$_SESSION['PhoneName'] = '';
							$_SESSION['PhoneCode'] = '';
							$_SESSION['kboy_user'] = $user_phone;
							$_SESSION['kboy_userid'] = $kboy_userid;
							$_SESSION['kboy_leader'] = getClassName("kboy_user","user_leader","( user_email='$user_phone' or user_phone='$user_phone' )");
							setcookie("username",$user_phone,time()+3600*24*365);
   							setcookie("password",getClassName("kboy_user","user_password","user_phone='$user_phone'"),time()+3600*24*365);
							echo ok("报名成功", "user_index.php");
						}else{
							echo erro("报名失败");
						}
					}
						
//				}else{
//					echo erro("请用本人电话号码，方便联系");
//				}
					
				
		}else{
			$mysql = new sqlstr("kboy_user"); 
			$mysql -> set("user_phone",$user_phone);
			$mysql -> set("user_realname",$user_realname);
			$mysql -> set("user_nickname",$user_nickname);
			$mysql -> set("user_password",$user_password);
			$mysql -> set("user_sex",$sex);
			$mysql -> set("user_idcard",$user_idcard);
			$mysql -> set("user_face","/images/lvyou.jpg");
			$news_sql = $mysql -> insertSql();
			$news_res = mysql_query($news_sql);
			$kboy_userid = mysql_insert_id();
			if($news_res){
				setcookie("username",$user_phone,time()+3600*24*365);
   				setcookie("password",$user_password,time()+3600*24*365);
				$mysql = new sqlstr("kboy_order"); 
				$mysql -> set("uid",$kboy_userid,true);
				$mysql -> set("order_phone",$user_phone);
				$mysql -> set("order_no",$order_no);
				$mysql -> set("order_realname",$user_realname);
				$mysql -> set("order_sex",$sex);
				$mysql -> set("order_idcard",$user_idcard);
				//性别
				$mysql -> set("order_shangche",$order_shangche);
				$mysql -> set("aid",$aid,true);
				$mysql -> set("order_num",1);
				
				$news_sql = $mysql -> insertSql();
			
				$news_res = mysql_query($news_sql);
				if($news_res){
					$_SESSION['PhoneName'] = '';
					$_SESSION['PhoneCode'] = '';
					$_SESSION['kboy_user'] = $user_phone;
					$_SESSION['kboy_userid'] = $kboy_userid;
					$_SESSION['kboy_leader'] = getClassName("kboy_user","user_leader","( user_email='$user_phone' or user_phone='$user_phone' )");
					echo ok("报名成功", "user_index.php");
				}else{
					echo erro("报名失败");
				}
			}else{
				echo "用户信息写入失败";
			}	
		}
	
		
		
	}
